//
//  AppDelegate.m
//  PokerHandUp
//
//  Created by psvn047 on 6/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#import "AppDelegate.h"
#import "SignInViewController.h"
#import "GameViewController.h"
#import "WelcomeViewController.h"
#import "MainScreenViewController.h"
#import "SignUpViewController.h"
#import "HelpPopupViewController.h"
#import "SettingPopupViewController.h"
#import "LeaderboardPopupViewController.h"
#import "ChipPopupViewController.h"
#import "TournamentViewController.h"
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@interface AppDelegate ()

@property (nonatomic, retain) UIWindow *applicationWindow;

@property (nonatomic, retain) NavViewController *freeNavigationController;
// screens
@property (nonatomic, retain) SignInViewController *signInViewController;
@property (nonatomic, retain) MainScreenViewController *mainScreenViewController;
@property (nonatomic, retain) WelcomeViewController *welcomeViewController;
@property (nonatomic, retain) GameViewController *gameViewController;
@property (nonatomic, retain) SignUpViewController *signUpViewController;
@property (nonatomic, retain) TournamentViewController *tournamentViewController;
//popups
@property (nonatomic, retain) HelpPopupViewController *helpPopupViewController;
@property (nonatomic, retain) SettingPopupViewController *settingPopupViewController;
@property (nonatomic, retain) LeaderboardPopupViewController *leaderboardPopupViewController;
@property (nonatomic, retain) ChipPopupViewController *chipPopupViewController;
@end
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@implementation AppDelegate
@synthesize applicationWindow = _applicationWindow;
@synthesize freeNavigationController = _freeNavigationController;
@synthesize deck = _deck;
@synthesize avatarCollection =_avatarCollection;
// screens
@synthesize welcomeViewController = _welcomeViewController;
@synthesize signInViewController = _signInViewController;
@synthesize gameViewController = _gameViewController;
@synthesize mainScreenViewController = _mainScreenViewController;
@synthesize signUpViewController = _signUpViewController;
@synthesize tournamentViewController = _tournamentViewController;
// popups
@synthesize helpPopupViewController = _helpPopupViewController;
@synthesize settingPopupViewController = _settingPopupViewController;
@synthesize leaderboardPopupViewController = _leaderboardPopupViewController;
@synthesize chipPopupViewController = _chipPopupViewController;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+ (AppDelegate *)sharedController
{
    return (AppDelegate *)[UIApplication sharedApplication].delegate;
}
///////////////////////////
// Show screens
//////////////////////////
- (void)showTournamentScreen
{
    if (nil == self.tournamentViewController)
    {
        self.tournamentViewController = [[TournamentViewController new] autorelease];
    }
    
    [self.freeNavigationController addTopViewController:self.tournamentViewController];
}
- (void)showSignInScreen
{
    if (nil == self.signInViewController)
    {
        self.signInViewController = [[SignInViewController new] autorelease];
    }
    
    [self.freeNavigationController addTopViewController:self.signInViewController];
}

- (void)showMainScreen
{
    if (nil == self.mainScreenViewController)
    {
        self.mainScreenViewController = [[MainScreenViewController new] autorelease];
    }
    
    [self.freeNavigationController addTopViewController:self.mainScreenViewController];
}

- (void)showWelcomeScreen
{
    if (nil == self.welcomeViewController)
    {
        self.welcomeViewController = [[WelcomeViewController new] autorelease];
    }
    
    [self.freeNavigationController addTopViewController:self.welcomeViewController];
}
- (void)showGameScreen
{
    if (nil == self.gameViewController)
    {
        self.gameViewController = [[GameViewController new] autorelease];
    }
    
    [self.freeNavigationController addTopViewController:self.gameViewController];
}
- (void)showSignUpScreen
{
    if (nil == self.signUpViewController)
    {
        self.signUpViewController = [[SignUpViewController new] autorelease];
    }
    
    [self.freeNavigationController addTopViewController:self.signUpViewController];
}
///////////////////////////
// Show popups
/////////////////////////
- (void)showHelpPopup
{
    if (nil == self.helpPopupViewController)
    {
        self.helpPopupViewController = [[HelpPopupViewController new] autorelease];
    }
    
    [self.freeNavigationController addTopPopupViewController:self.helpPopupViewController];
}
- (void)showLeaderboardPopup
{
    if (nil == self.leaderboardPopupViewController)
    {
        self.leaderboardPopupViewController = [[LeaderboardPopupViewController new] autorelease];
    }
    
    [self.freeNavigationController addTopPopupViewController:self.leaderboardPopupViewController];
}
- (void)showChipPopup
{
    if (nil == self.chipPopupViewController)
    {
        self.chipPopupViewController = [[ChipPopupViewController new] autorelease];
    }
    
    [self.freeNavigationController addTopPopupViewController:self.chipPopupViewController];
}
- (void)showSettingPopup
{
    if (nil == self.settingPopupViewController)
    {
        self.settingPopupViewController = [[SettingPopupViewController new] autorelease];
    }
    
    [self.freeNavigationController addTopPopupViewController:self.settingPopupViewController];
}
- (void)closePopup
{
    [self.freeNavigationController closePopupViewController];
}
- (void)dealloc
{
    self.applicationWindow = nil;
    self.freeNavigationController = nil;
    self.deck = nil;
    self.avatarCollection = nil;
    //screens
    self.signInViewController = nil;
    self.gameViewController = nil;
    self.welcomeViewController = nil;
    self.mainScreenViewController = nil;
    self.signUpViewController = nil;
     self.tournamentViewController = nil;
    //popups
    self.helpPopupViewController = nil;
    self.settingPopupViewController = nil;
    self.leaderboardPopupViewController = nil;
    self.chipPopupViewController = nil;
    
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    CGRect windowFrame = [UIScreen mainScreen].bounds;
    self.applicationWindow = [[[UIWindow alloc] initWithFrame:windowFrame] autorelease];
    self.freeNavigationController = [[NavViewController new] autorelease];
    self.applicationWindow.rootViewController = self.freeNavigationController;
    [self.applicationWindow makeKeyAndVisible];
    
   NSString *deckFilename = [[NSBundle mainBundle] pathForResource:@"deck1" ofType:@"xml"];
    self.deck = [[[Deck alloc] initWithDeckFilename:deckFilename] autorelease];
    NSString *avatarsCollectionFilename = [[NSBundle mainBundle] pathForResource:@"avatars" ofType:@"xml"];
    self.avatarCollection = [[[AvatarCollection alloc] initWithAvatarCollectionFilename:avatarsCollectionFilename] autorelease];
    [self showWelcomeScreen];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
